/**
 * 初始化 iframe 数据
 */

import './XHRreload'
const initXHRScript1 = () => {
    /**
     * 添加 iframe
     * @param {string} id iframe id
     * @param {string} pagePath iframe 路径
     */
    console.log('开始准备插入')
    const addScript = (id: string, pagePath: string) => {
        const contentScript = document.createElement('script')
        contentScript.id = id
        const getContentPage = chrome.runtime.getURL(pagePath)
        contentScript.src = getContentPage
        contentScript.type = 'text/javascript'
        contentScript.onload = function () {
            // const _this = this as HTMLScriptElement
            // _this.remove();

            chrome.storage.local.get(['initTopData', 'initJinyingData', 'initTiyangfenData', 'initDslpData'], function (result) {
                console.log(result.initTopData);
                if (result.initTopData) {
                    window.postMessage({
                        type: 'setXhrData',
                        data: {
                            initTopData: result.initTopData,
                            initJinyingData: result.initJinyingData,
                            initTiyangfenData: result.initTiyangfenData,
                            initDslpData: result.initDslpData
                        }
                    }, '*')
                }
              });
          

        };
        // document.head.appendChild(contentScript)
        document.head.insertBefore(contentScript, document.head.firstChild)
    }

    addScript('content-chr', 'contentXHR/XHRreload.js.js')
}

// 判断 window.top 和 self 是否相等，如果不相等，则不注入 iframe
if (window.top == window.self) {
    console.log('content-chr')
    initXHRScript1()
}